/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.BankLoginService;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;

/**
 * 作者：韩天
 * 创建时间：2011-8-27
 * 类描述：通用登录模块的action封装类
 */
public class LoginAction extends ActionSupport {
    
    private String userName;//登陆账号
    private String password;//登陆密码
    
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute() {
        return SUCCESS;
    }
    
    /**
     * 方法描述：登录
     * param：
     * return："bank"——登陆用户为银行人员
     *          "enterprise"——登陆用户为企业人员， INPUT——用户名密码错误，重新登陆
     */
    public String login() {
        EnterpriseService es = ServiceFactory.getEnterpriseServiceInstance();
        //判断用户名是否存在
        if(es.getEnterpriseByUsername(userName) != null){
            Enterprise enterprise = es.getEnterpriseByUsername(userName);
            //判断密码是否正确
            if(enterprise.getPassword().equals(password)){
                //取得enterprise对象
                enterprise = es.getEnterpriseByUsername(userName); 
                //将企业id放入session中
                ActionContext actionContext = ActionContext.getContext();  
                Map session = actionContext.getSession();
                session.put("userId", String.valueOf(enterprise.getId()));
                session.put("userName", enterprise.getName());
             
                return "enterprise";
            }
        }else{
            BankLoginService bls = ServiceFactory.getBankLoginServiceInstance();
            if(bls.isLogin(userName, password)){
                ActionContext actionContext = ActionContext.getContext();  
                Map session = actionContext.getSession();
                session.put("userId", "-1");
                session.put("userName", "bank");
                         
                return "bank";
            }
        }
        
        //用户名不存在或密码错误，重新登陆
        return INPUT;
    }
    
    
    

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
